﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WeixinMP.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.WeixinMP.DataAccess;
using Microhard.WeixinMP.DTO;
using Microhard.Core;
using Microhard.WeixinMP.Msg;
using Microhard.WeixinMP.Models;

namespace Microhard.WeixinMP.Impls
{
    public class ArticleImpl : IArticle
    {
        public Core.DBUtility.ExecutedResult Add(Models.ArtileModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;
            op.CompanyID = this.scope.EmpInfo.CompanyID;
            op.Type = op.CompanyID + "_" + model.Type;
            try
            {
                db.Articles.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, ArticleMsg.AddFailed);
                Log.Error("IArticle.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Models.ArtileModel model)
        {
            ExecutedResult result = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            var entity = db.Articles.SingleOrDefault(l => l.ArticleID == model.ArticleID);
            if (entity != null)
            {
                entity.Title = model.Title;
                entity.ArticleContent = model.ArticleContent;
                entity.Type = entity.CompanyID + "_" + model.Type;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, ArticleMsg.UpdateFailed);
                Log.Error("IArticle.Update", ex);
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            var toDeltes = from b in db.Articles
                           where ids.Contains(b.ArticleID)
                           select b;
            foreach (var t in toDeltes)
            {
                if (t.ShareLinks.Count == 0)
                {
                    db.Articles.DeleteObject(t);
                }
                else
                {
                    result.ErrorMsg.Add("IsUsing", string.Format(ArticleMsg.ArticleIsInUsing, t.Title));
                }
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, ArticleMsg.DeleteFailed);
                    Log.Error("IArticle.Delete", ex);
                }
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(Models.ArtileModel model)
        {
            throw new NotImplementedException();
        }

        public Models.ArtileModel GetModelByID(Guid id)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            var fb = db.Articles.SingleOrDefault(i => i.ArticleID == id);
            if (fb != null)
            {
                return fb.ToModel();
            }
            return null;
        }

        public Models.ArtileModel GetModelByModel(Models.ArtileModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.ArtileModel> Query(Models.ArtileModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            IEnumerable<Article> entities = from b in db.Articles
                                            select b;
            if (!string.IsNullOrEmpty(Model.Title))
            {
                entities = from b in db.Articles
                           where b.Title.StartsWith(Model.Title) == true
                           select b;
            }
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<ArtileModel>();
        }

        public List<Models.ArtileModel> Query(Models.ArtileModel Model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }

        public List<string> GetArticleTypes()
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            return db.Articles.Where(a => a.CompanyID == this.scope.EmpInfo.CompanyID)
                .Select(p => p.Type).Distinct().ToList();
        }




        public List<ArtileModel> ListPageView(ArtileModel model)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            var arts = from a in db.Articles
                       where a.CompanyID == model.CompanyID && a.Type == model.Type
                       select a;
            return arts.Select(s => new ArtileModel
            {
                ArticleID = s.ArticleID,
                CompanyID = s.CompanyID.Value,
                CreateTime = s.CreateTime.Value,
                Title = s.Title,
                Type = s.Type,
                WeixinName = model.WeixinName
            }).ToList();
        }
    }
}
